from pymongo import MongoClient
import pymysql.cursors
from util import de

MYSQL_CONN = pymysql.connect(host='192.168.0.220',
                             user='root',
                             password='root',
                             db='crunchbase',
                             charset='utf8mb4',
                             cursorclass=pymysql.cursors.DictCursor)

CLIENT = MongoClient('192.168.0.220', 27017)
DB = CLIENT['crunchbase']
COLLECTION = DB['Organization']

pipeline = [
{'$group': {'_id': {'permalink': "$permalink"}, 'uniqueIds': {'$addToSet': "$_id"}, 'count': {'$sum': 1}}},
{'$match': {'count': {'$gt': 1}}}
]
pipeline2 = [
{'$group': {'_id': {'permalink': "$permalink"}, 'uniqueIds': {'$addToSet': "$_id"}, 'count': {'$sum': 1}}},
{'$match': {'count': {'$gt': 5}}}
]

re = COLLECTION.aggregate(pipeline)
# re = collection.find()




def merge_2():
    '''
    合并2个文档
    :return:
    '''
    for collec in re:
        if collec.get('count') == 2:
            og1 = COLLECTION.find_one({'_id':collec.get('uniqueIds')[0]})
            og2 = COLLECTION.find_one({'_id':collec.get('uniqueIds')[1]})
            de(og1)
            de(og2)
            if not og1.get('uuid') and not og2.get('uuid'):
                continue
            if not og1.get('uuid') and og2.get('uuid'):
                if og1.get('api_path') == og2.get('api_path'):
                    og1.update(og2)
                    COLLECTION.update_one({'_id':collec.get('uniqueIds')[1]}, {'$set':og1})
                    COLLECTION.delete_one({'_id':collec.get('uniqueIds')[0]})
                continue
            if not og2.get('uuid') and og1.get('uuid'):
                if og1.get('api_path') == og2.get('api_path'):
                    og2.update(og1)
                    COLLECTION.update_one({'_id': collec.get('uniqueIds')[0]}, {'$set': og2})
                    COLLECTION.delete_one({'_id': collec.get('uniqueIds')[1]})
                continue
            if not og2.get('uuid') == og1.get('uuid'):
                og2.update(og1)
                COLLECTION.update_one({'_id': collec.get('uniqueIds')[0]}, {'$set': og2})
                COLLECTION.delete_one({'_id': collec.get('uniqueIds')[1]})
                continue
        # print(collec)


def merge_3():
    '''
    合并3个文档
    :return:
    '''
    for collec in re:
        if collec.get('count') < 100:
            ogs = [COLLECTION.find_one({'_id':collec.get('uniqueIds')[i]}) for i in range(len(collec.get('uniqueIds')))]
            uuids = set()
            apis = set()
            for og in ogs:
                de(og)
                uuids.add(og.get('uuid'))
                apis.add(og.get('api_path'))
                print(og)
            exit()
            # print(uuids)
            # print(uuids)
            print(ogs)
            if len(uuids)==1 and len(apis) == 1:
                for i, og in enumerate(ogs):
                    ogs[0].update(og)
                    COLLECTION.delete_one({'_id': collec.get('uniqueIds')[i]})
                COLLECTION.insert_one(ogs[0])


def get_uuid():
    '''
    给mongodb 没有uuid 的添加
    :return:
    '''
    db = CLIENT['crunchbase']
    collection = db['Organization']
    res = collection.find({'uuid':None})
    for each in res:
        with MYSQL_CONN.cursor() as cursor:
            sql = "SELECT `uuid` FROM `organization` WHERE api_path = %s"
            cursor.execute(sql, each.get('api_path'))
            re_uuid = cursor.fetchone().get('uuid')
            if re_uuid:
                each['uuid'] = re_uuid
                collection.update_one({'_id':each.get('_id')},{'$set':each})
            print(re_uuid)

if __name__ == '__main__':
    # merge_3()
    # get_uuid()
    pass